%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define YYSTYPE char*
char idstr[50];
char numstr[50];
int yylex();
extern int yyparse();
FILE* yyin;
void yyerror(const char*s);
%}
%token NUMBER
%token ID
%token ADD SUB MUL DIV LB RB
%left LB
%left ADD SUB
%left MUL DIV
%right UMINUS
%right RB
%%

lines : lines expr ';'{printf("%s\n",$2);}
      | lines ';'
      |
      ;

expr : expr ADD expr {$$=(char*)malloc(50*sizeof(char)); strcpy($$,$1);
                      strcat($$,$3);strcat($$,"+"); }
     | expr SUB expr {$$=(char*)malloc(50*sizeof(char)); strcpy($$,$1);
                      strcat($$,$3);strcat($$,"-"); }
     | expr MUL expr {$$=(char*)malloc(50*sizeof(char)); strcpy($$,$1);
                      strcat($$,$3);strcat($$,"*"); }
     | expr DIV expr {$$=(char*)malloc(50*sizeof(char)); strcpy($$,$1);
                      strcat($$,$3);strcat($$,"/"); }
     | LB expr RB {$$=(char*)malloc(50*sizeof(char));strcpy($$,$2);}
     | NUMBER   {$$ = (char*)malloc(50*sizeof(char)); strcpy($$,$1);
                 strcat($$," ");}
     | ID   {$$=(char*)malloc(50*sizeof(char));
             strcpy($$,$1);strcat($$," ");}


%%
int yylex()
{
      char t;
    
	while (1){
		t = getchar();
		if (t == ' ' || t == '\t'||t=='\n')
		        {}
                else if (t == '+')
		        return ADD;
                else if (t == '-')
                        return SUB;
                else if (t == '*')
                        return MUL;
                else if (t == '/')
                        return DIV;
                else if (t == '(')
                        return LB;
                else if (t == ')')
                        return RB;
                        else if (isdigit(t)) {
                        int ti=0;
                        while(isdigit(t)){
                                numstr[ti]=t;
                                t = getchar();
                                ti++;
                        }
                        ungetc(t, stdin);
                        numstr[ti]='\0';
                        yylval=numstr;
                        return NUMBER;
                        }
                else if ((t >= 'a' && t <= 'z' ) || (t >= 'A' && t <= 'Z') || (t == '_')) {
                        int ti=0;
                        while(('a'<=t&&t<='z')||('A'<=t&&'Z'>=t)||(t=='_')||(t>='0'&&t<='9')){
                        idstr[ti] = t;
                        ti++;
                        t=getchar(); 
                        }
                        idstr[ti] = '\0';
                        ungetc(t,stdin);
                        yylval=idstr;
                        return ID; 
                }
                else {return t; }
        }
}

int main(void)
{
   yyin = stdin;
  do{
      yyparse();
    }while(!feof(yyin));
  return 0;
}
void yyerror(const char* s)
{
   fprintf(stderr,"Parse error:%s\n",s);
   exit(1);
}
